/**
 * Created by YJH on 2018/5/29.
 */
import React, {Component} from 'react';
import {
    StyleSheet,
    Text,
    View,
    TouchableOpacity,
    Image,
    Button,
    TextInput,
    ListView,
    RefreshControl,
} from 'react-native';
import DataRespository from '../../expand/dao/DataRespository';
import RespositoryCell from '../../common/RespositoryCell';
import Toast, {DURATION} from "react-native-easy-toast";


const URL = 'https://api.github.com/search/repositories?q=';
const SEARCH_STR = '&sort=stars';
export default class JavaPage extends Component {
    constructor(props) {
        super(props);
        this.dataRespository = new DataRespository();
        this.state = ({
            searchTxt: 'java',
            dataSource: new ListView.DataSource({
                rowHasChanged: (r1, r2) => r1 !== r2
            }),
        })
    }

    genUrl(search_key) {
        return URL + search_key + SEARCH_STR;
    }

    onLoad(url) {
        this.dataRespository.fetNetRespository(url)
            .then(result => {
                this.setState({
                    dataSource: this.state.dataSource.cloneWithRows(result.items),
                });
            })
    }

    componentDidMount() {
        //页面装载之后调用
        this.onLoad(this.genUrl(this.state.searchTxt))
    }

    renderRow(data) {
        return <RespositoryCell data={data}/>
    }

    renderSeparator(sectionID, rowID, adjacentRowHighlighted) {
        return <View style={styles.item_line}>
        </View>
    }

    render() {
        return <View style={styles.item_view}>
            <ListView
                dataSource={this.state.dataSource}
                renderRow={(data) => this.renderRow(data)}
                renderSeparator={(sectionID, rowID, adjacentRowHighlighted) => this.renderSeparator(sectionID, rowID, adjacentRowHighlighted)}
            />
        </View>
    }

}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: 'white',
    },
    boy_text: {
        fontSize: 18,
        color: 'black',
        alignSelf: 'center',
    },
    tab_image: {
        width: 20,
        height: 20,
    },
    text: {
        marginTop: 50,
        fontSize: 18,
        marginTop: 10,
        alignSelf: 'center',
    },
    button: {
        height: 60,
        marginTop: 30,
        fontSize: 20,
        alignSelf: 'center',
    },
    edt: {
        height: 50,
        marginTop: 30,
        minWidth: 300,
        alignSelf: 'center',
        borderColor: '#02F78E',
    },
    item_text: {
        fontSize: 16,
        color: 'black',
    },
    item_row: {
        height: 60
    },
    item_line: {
        height: 1,
        backgroundColor: '#FCFCFC'
    },
    item_image: {
        height: 100,
        width: 200,
        tintColor: 'red',
        justifyContent: 'center',
        alignSelf: 'center',
    },
    item_view: {
    }
});